using System.Text;
using System.Collections.Generic;
using System.IO;
using Framework.DataTable;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.EventSystems;
using Framework.Bundle;

namespace Framework
{

    public partial class ToolsMenu
    {
        [MenuItem("Window/回到初始场景 %#L", false, 3)]
        private static void startEntryScene()
        {
            if (EditorBuildSettings.scenes.Length < 1) return;
            var scene = EditorBuildSettings.scenes[0];
            EditorSceneManager.OpenScene(scene.path, OpenSceneMode.Single);
            var asset = AssetDatabase.LoadMainAssetAtPath(scene.path);
            if (asset != null)
            {
                // EditorGUIUtility.PingObject(asset);
            }
        }
        // [MenuItem("Window/*自动生成UIPathInfo")]
        // private static void createUIInfo()
        // {
        // }

        [MenuItem("Window/工具/数据表生成 %#E")]
        private static void CreateTable()
        {
            /**
            #配制表	index可创建索引		cs可创结构分支		可创建数据分支	适用于多语言	
            #define	cs	c	cs	cs	c	c	c
            #name	Id	Name	Open	Time	Text#ch	Text#en	PList
            #type	index#int	string	bool	float	string	string	int[]
            #备注							
                    3	c	1	100	二	two	{1,2,3}
                    4	d	0	66	三	three	{1,2,3} 0 
            **/
            var pip = new Framework.DataTable.TableBuildPipline();
            pip.Execute();
        }

        [MenuItem("Window/工具/生成配制文件 ")]
        private static void CreateSetting()
        {
            FrameworkSetting.CreateSetting();
        }
        [MenuItem("Window/工具/生成link.xml")]
        private static void CreateLink()
        {
            Linker.Instance.Execute(true);
        }
        [MenuItem("Window/工具/资源构建 %#M")]
        private static void CreateBundle()
        {
            var pip = new BuildPipline();
            pip.Execute();
        }
        [MenuItem("Window/工具/资源配制")]
        private static void AssetBundleEditer()
        {
            AssetBundleEditor.CreateWindow();
        }
        [MenuItem("Window/工具/shader关键字编辑")]
        private static void ShaderEditer()
        {
            ShaderEditor.CreateWindow();
        }
        [MenuItem("Window/工具/逐帧运行 %RIGHT")]
        private static void AppStep()
        {
            EditorApplication.Step();
        }
        /*
        [MenuItem("Window/工具/恢复运行 %LEFT")]
        private static void AppPlay()
        {
            EditorApplication.isPaused = false;
        }
        */
        [MenuItem("Window/工具/捕获GUI节点 %Q")]
        private static void FindGui()
        {
            if (!EditorApplication.isPlaying) return;
            PointerEventData pointer = new PointerEventData(EventSystem.current);
            pointer.position = Input.mousePosition;
            List<RaycastResult> results = new List<RaycastResult>();
            EventSystem.current.RaycastAll(pointer, results);
            if (results.Count > 0)
            {
                var clickObj = results[0].gameObject;
                UnityEditor.Selection.activeGameObject = clickObj;
                UnityEditor.EditorGUIUtility.PingObject(clickObj);
            }
        }
        [MenuItem("Window/工具/对象池信息")]
        private static void openCustomPoolInfo()
        {
            CustomPoolInfo.ShowWindow();
        }
        [MenuItem("Window/工具/测试代码 %#T")]
        private static void Test()
        {
            //0123456789|
            //0456789|
            List<System.Type> typeNames = new List<System.Type>();
            var assemblyNames = new string[] { "Assembly-CSharp-Editor" };
            var typeBase = typeof(IFrameworkTest);
            foreach (string assemblyName in assemblyNames)
            {
                System.Reflection.Assembly assembly = System.Reflection.Assembly.Load(assemblyName);
                if (assembly == null)
                {
                    continue;
                }
                System.Type[] types = assembly.GetTypes();
                foreach (System.Type type in types)
                {
                    if (type.IsClass && !type.IsAbstract && typeBase.IsAssignableFrom(type))
                    {
                        typeNames.Add(type);
                    }
                }
            }
            foreach (var item in typeNames)
            {
                if (System.Activator.CreateInstance(item) is IFrameworkTest cmd)
                {
                    cmd.Execute();
                }
            }
        }
        /*
        private const string LOADMODEMENU = "Window/调试/AssetsBundle模式";
        private const string AssetsBundleMode = "AssetsBundleMode";
        [MenuItem(LOADMODEMENU)]
        private static void LoadMode()
        {
            var enabled = !EditorPrefs.GetBool(AssetsBundleMode);
            EditorPrefs.SetBool(AssetsBundleMode, enabled);
            Menu.SetChecked(LOADMODEMENU, enabled);
            if (enabled)
            {
                Debug.LogError("开启bundle加载模式");
            }
            else
            {
                Debug.LogError("关闭bundle加载模式");
            }

        }
        */
    }
}